<?php
namespace app\index\command;

use service\TreeService;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\Db;
use think\Request;

use service\GuardService;

class CronGuard extends Command
{
    /**
     * 基础配置信息
     */
    protected function configure()
    {
        parent::configure();

        $this->setName('cronguard')
            ->setDescription('更新门禁系统部门或员工')
            ->setHelp('php think cronguard 更新类型（1部门  2员工）；')
            ->addArgument('utype');
    }

    /**
     * 执行体
     * @param Input $input
     * @param Output $output
     * @return int|void|null
     */
    protected function execute(Input $input, Output $output)
    {
        $utype = $input->getArgument('utype');
        if (empty($utype)) {
            $utype = 1;
        }
        if (!empty($utype) && !in_array($utype, [1, 2])) {
            exit("参数错误！1代表部门更新，2代表员工更新" . PHP_EOL);
        }
        //echo $utype.PHP_EOL;exit;
        $starttime = explode(' ',microtime());
        echo "start".PHP_EOL;
        $ctime = time();
        /*$main_department_lists = Db::name("department")->where(['status'=>1])
            //->where(['is_jtzb'=>1])
            ->order("pid asc,sort asc")->select();*/
        $main_department_lists = Db::name("department")
            ->where(['status'=>1])
            ->where(['update_guard_state'=>1])
            ->order("pid asc,sort asc")->select();
        //dump($main_department_lists);exit;
        $treelists = TreeService::listToTree($main_department_lists,'department_id', $pid = 'pid', $child = '_child', $root = 99990176);
        //dump(count($treelists));exit;
        //dump(count(getLeafNodes($treelists)));exit;
        $count = 1;
        foreach (getLeafNodes($treelists) as $row){
            $res = GuardService::instance()->DelDepartment($row['department_id']);
            //dump($res);exit;
            echo $count++."--".$row['department_id']."--".$res['code']."--".$res['message'].PHP_EOL;
        }
        exit;
        if($utype == 1){
            //dump(count($main_department_lists));exit;
            foreach ($main_department_lists as $main_department_list){
                if($main_department_list['pid'] == 0){
                    $main_department_list['pid'] = 1;
                }
                $res = GuardService::instance()->AddDepartment($main_department_list);
                $u_data = [];
                if($res['code'] == 0 && $res['message'] == 'success'){
                    $u_data['update_guard_state'] = 1;
                }
                else{
                    $u_data['update_guard_state'] = 0;
                }
                $u_data['update_guard_res'] = json_encode($res,304);
                Db::name("department")->where(['id'=>$main_department_list['id']])->update($u_data);
            }
        }
        else{
            //$departmentlists = Db::name("user")->select();
            $departmentlists = $main_department_lists;
            foreach ($departmentlists as $departmentlist){
                FeishuService::instance()->GetuserBydeparmentid($departmentlist['department_id'],$ctime);
            }
            Db::name("user")->where(['update_time'=>['<>',$ctime]])->update(['update_state'=>0]);
        }
        $endtime = explode(' ',microtime());
        $thistime = $endtime[0]+$endtime[1]-($starttime[0]+$starttime[1]);
        $thistime = round($thistime,3);
        echo "本次执行耗时：".$thistime." 秒。".PHP_EOL;
    }

}